package com.example.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;


public class AppResult<T> {
    /*
    * 1，属性加⼊@JsonInclude(JsonInclude.Include.ALWAYS)表⽰⽆论是否为空必须序列化
    * */
    //状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private long code;
    //状态描述
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;

    //结果数据（博客系统使用的是Object）
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    /*
    * 构造方法
    * */

    public AppResult(long code, String message) {
        this(code,message,null);
    }

    public AppResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 无data
     * @return
     */
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    /**
     * 返回data
     * @param data
     * @param <T>
     * @return
     */
    public static <T> AppResult<T> success(T data){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    /**
     * 返回data以及自定义描述message
     * @param message
     * @param data
     * @param <T>
     * @return
     */
    public static <T> AppResult<T> success(String message,T data){
        return new AppResult(ResultCode.SUCCESS.getCode(), message,data);
    }


    /*
    * 统一返回格式
    * 失败
    * */

    //无参数时，返回的样子
    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }

    //自定义message
    public static AppResult failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(),message);
    }

    //返回数据data
    public static <T> AppResult<T> failed(T data){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage(),data);
    }

    //返回错误状态
    public static AppResult failed(ResultCode resultCode){
        return new AppResult(resultCode.getCode(),resultCode.getMessage());
    }

    //返回枚举中的错误状态及结果数据data
    public static <T> AppResult<T> failed(ResultCode resultCode,T data){
        return new AppResult(resultCode.FAILED.getCode(),resultCode.FAILED.getMessage(),data);
    }

    /*
    * Getter and Setter
    * */
    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
